<?php
/**
 * 任务列表
 * author itholiday@126.com
 */
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';
//页面显示条数
$pagesize = 15;
$page = max(1, $_GPC['page']);
$start = ($page - 1) * $pagesize;

$where = 'A.id>0 ';

$title = isset($_GPC['title']) && !empty($_GPC['title']) ? $_GPC['title'] : '';
$taskid = isset($_GPC['taskid']) && !empty($_GPC['taskid']) ? $_GPC['taskid'] : '';
$start_time = isset($_GPC['start_time']) && !empty($_GPC['start_time']) ? strtotime($_GPC['start_time']) : '';
$end_time = isset($_GPC['end_time']) && !empty($_GPC['end_time']) ? strtotime($_GPC['end_time']) : '';

if($taskid != ''){
    $where .= " and A.id=$taskid ";
}
if($title != ''){
    $where .= " and A.title like '%$title%' ";
}
if (!empty($start_time) && !empty($end_time)) {
    $where .= " and (A.begin_time>=$start_time and A.end_time<=$end_time) ";
} else {
    if (!empty($start_time)) {
        $where .= " and (A.begin_time>=$start_time) ";
    }
    if (!empty($end_time)) {
        $where .= " and (A.end_time<=$end_time) ";
    }
}
if ($op == 'add') {
    if (is_ajax()) {
        $data['title'] = trim($_GPC['title']);
        $data['remark'] = trim($_GPC['editorValue']);
        $data['validate'] = trim($_GPC['validate']);
        $data['thumb'] = cmf_asset_relative_url(trim($_GPC['thumb']));
        $data['task_link'] = trim($_GPC['task_link']);
        $data['news_from'] = trim($_GPC['news_from']);
        $data['release_num'] = intval($_GPC['release_num']);
        $data['receive_num'] = intval($_GPC['receive_num']);
        $data['cateid'] = intval($_GPC['cateid']);
        $data['task_money'] = floatval($_GPC['task_money']);
        $data['begin_time'] = strtotime($_GPC['begin_time']);
        $data['end_time'] = strtotime($_GPC['end_time']);
        $data['create_time'] = time();
        $data['create_id'] = $_W['uid'];
        $res = pdo_insert('yq_haircircle_task', $data);
        if (!empty($res)) {
            ajaxSuccess('发布成功');
        }
        ajaxError('发布失败');
    }
    $field = "id,pid,name,soncate,lv,sorts,isopen";
    $map = 'id>0 ';
    $catesql = "select $field from ims_yq_haircircle_task_cate where $map order by pid,sorts,id desc";
    $catedata = pdo_fetchall($catesql);
    $catelist = getTree($catedata);
    include $this->template("web/task_list_add");
}

if ($op == 'edit') {
    $id = intval($_GPC['id']);
    $task = pdo_get('yq_haircircle_task', array('id' => $id));
    if (is_ajax()) {
        $res = pdo_get('yq_haircircle_task_receive', array('taskid' => $id));
        if (!empty($res) && (strtotime($_GPC['begin_time'])!=$task['begin_time'] || strtotime($_GPC['end_time']!=$task['end_time']))) {
            ajaxError('已经有人接受任务了,不要更改时间了');
        }
        $data['title'] = trim($_GPC['title']);
        $data['remark'] = trim($_GPC['editorValue']);
        $data['validate'] = trim($_GPC['validate']);
        $data['thumb'] = cmf_asset_relative_url(trim($_GPC['thumb']));
        $data['task_link'] = trim($_GPC['task_link']);
        $data['news_from'] = trim($_GPC['news_from']);
        $data['release_num'] = intval($_GPC['release_num']);
        $data['receive_num'] = intval($_GPC['receive_num']);
        $data['cateid'] = intval($_GPC['cateid']);
        $data['task_money'] = floatval($_GPC['task_money']);
        $data['begin_time'] = strtotime($_GPC['begin_time']);
        $data['end_time'] = strtotime($_GPC['end_time']);
        $data['update_time'] = time();
        $data['update_id'] = $_W['uid'];
        $res = pdo_update('yq_haircircle_task', $data, array('id' => $id));
        if (!empty($res)) {
            ajaxSuccess('保存成功');
        }
        ajaxError('保存失败');
    }
    $field = "id,pid,name,soncate,lv,sorts,isopen";
    $map = 'id>0 ';
    $catesql = "select $field from ims_yq_haircircle_task_cate where $map order by pid,sorts,id desc";
    $catedata = pdo_fetchall($catesql);
    $catelist = getTree($catedata);
    include $this->template("web/task_list_edit");
    exit;
}

//删除后，正在进行的任务就失效了
if ($op == 'del') {
    if (isset($_GPC['ids'])) {
        $ids = implode(',', $_GPC['ids']);
        pdo_begin();
        $sql = "update ims_yq_haircircle_task set delete_time=" . time() . ",delete_id=" . $_W['uid'] . " where id in ($ids)";
        $res = pdo_query($sql);
        if (empty($res)) {
            pdo_rollback();
            ajaxSuccess('删除失败');
        }
        $sql = "select * from ims_yq_haircircle_task_receive where taskid in ($ids) and receive_status=0";
        $list = pdo_fetchall($sql);
        if(!empty($list)){
            $sql = "update ims_yq_haircircle_task_receive set receive_status=1 where taskid in ($ids) and receive_status=0";
            $res = pdo_query($sql);
            if(empty($res)){
                pdo_rollback();
                ajaxSuccess('更新任务接取状态失败导致删除失败');
            }
        }
        pdo_commit();
        ajaxSuccess('删除成功');
    }
    if (isset($_GPC['id'])) {
        $id = $_GPC['id'];
        pdo_begin();
        $res = pdo_update('yq_haircircle_task', ['delete_time' => time(), 'delete_id' => $_W['uid']], array('id' => $id));
        if (empty($res)) {
            pdo_rollback();
            ajaxError('删除失败,请稍后重试');
        }
        $sql = "select * from ims_yq_haircircle_task_receive where taskid=$id and receive_status=0";
        $list = pdo_fetchall($sql);
        if(!empty($list)){
            $sql = "update ims_yq_haircircle_task_receive set receive_status=1 where taskid=$id and receive_status=0";
            $res = pdo_query($sql);
            if(empty($res)){
                pdo_rollback();
                ajaxSuccess('更新任务接取状态失败导致删除失败');
            }
        }
        pdo_commit();
        ajaxSuccess('删除成功');
    }
}

if ($op == 'upload_thumb') {
    if (is_post()) {
        $file = $_FILES["file"];
        if (!$file) {
            ajaxError('请上传图片');
        }
        $allowedExts = array("jpeg", "jpg", "png");
        $temp = explode("/", $file["type"]);
        $extension = end($temp);

        if (!in_array($extension, $allowedExts)) {
            ajaxError('只能上传图片文件');
        }
        if ($file["size"] > 4 * 1024 * 1024) {
            ajaxError('图片大小超过4M');
        }
        $file_dir = '../attachment/common/image/' . date('Ymd');//web文件夹的上层即为根目录
        if (!file_exists($file_dir)) {
            mkdirs($file_dir, '757');
        }
        $filename = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
        $res = move_uploaded_file($file["tmp_name"], $filename);
        if (!$res) {
            @unlink($filename);
            ajaxError('上传失败，稍后重试');
        }
        $imgurl = substr($filename, 14);
        if ($file['size'] > 2 * 1024 * 1024) {
            $new_path = $file_dir . '/' . md5(time() . uniqid()) . '.' . $extension;
            $res = image_png_size_add($filename, $new_path);
            if (!$res) {
                @unlink($filename);
                ajaxError('上传失败');
            }
            $imgurl = substr($new_path, 14);
        }
        ajaxSuccess('上传成功', cmf_get_upload_url($imgurl));
    }
}
if ($op == 'list' || $op == '') {

    $where .= " and A.delete_time=0 ";
    $total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' . tablename('yq_haircircle_task') . ' A WHERE ' . $where);

    //生成分页HTML
    $pager = getPageHTML($total, $page, $pagesize);
    $sql = "select A.* from ims_yq_haircircle_task A where $where and A.delete_time=0 order by A.id desc limit $start,$pagesize";
    $list = pdo_fetchall($sql);
    include $this->template('web/task_list');
    exit;
}